import type { App } from 'vue'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Layout from '@renderer/layout/index.vue'

/** 扩展的路由配置类型 */
export type AppRouteRecordRaw = RouteRecordRaw & {
  hidden?: boolean
}

/** 首页路径常量 */
export const HOME_PAGE = '/home'

/** 静态路由配置 */
const staticRoutes: AppRouteRecordRaw[] = [
  {
    path: '/',
    redirect: HOME_PAGE,
    component: Layout,
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('@renderer/views/index/index.vue'),
        meta: { title: 'menus.home.title' }
      },
      {
        path: 'history',
        name: 'history',
        component: () => import('@renderer/views/history/index.vue'),
        meta: { title: 'menus.history.title' }
      },
      {
        path: 'settings',
        name: 'settings',
        component: () => import('@renderer/views/settings/index.vue'),
        meta: { title: 'menus.settings.title' }
      },
      {
        path: 'help',
        name: 'help',
        component: () => import('@renderer/views/help/index.vue'),
        meta: { title: 'menus.help.title' }
      }
    ]
  }
]

/** 创建路由实例 */
export const router = createRouter({
  history: createWebHashHistory(),
  routes: staticRoutes,
  scrollBehavior: () => ({ left: 0, top: 0 })
})

/**
 * 初始化路由，将 Vue Router 实例挂载到 Vue 应用中
 * @param app Vue 应用实例
 */
export function setupRouter(app: App<Element>): void {
  app.use(router)
}
